<template>
  <div class="learning-analysis">
    <!-- 筛选条件 -->
    <el-form :inline="true" :model="filterForm" class="filter-form">
      <el-form-item label="统计时间">
        <el-date-picker
          v-model="filterForm.dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
          :shortcuts="dateShortcuts"
        />
      </el-form-item>
      <el-form-item label="培训科目">
        <el-select v-model="filterForm.subject" placeholder="请选择培训科目" clearable>
          <el-option label="科目一" value="subject1" />
          <el-option label="科目二" value="subject2" />
          <el-option label="科目三" value="subject3" />
          <el-option label="科目四" value="subject4" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleFilter">查询</el-button>
        <el-button @click="resetFilter">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 统计卡片 -->
    <el-row :gutter="20" class="statistics-cards">
      <el-col :span="6" v-for="card in statisticsCards" :key="card.title">
        <el-card shadow="hover">
          <div class="statistics-card-content">
            <div class="card-icon" :style="{ backgroundColor: card.color }">
              <el-icon>
                <component :is="card.icon" />
              </el-icon>
            </div>
            <div class="card-info">
              <div class="card-value">{{ card.value }}</div>
              <div class="card-title">{{ card.title }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 学习时长趋势图 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>学习时长趋势</span>
              <el-radio-group v-model="trendTimeRange" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
                <el-radio-button label="year">本年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div class="chart-container" ref="trendChartRef"></div>
        </el-card>
      </el-col>

      <!-- 学习进度分布图 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>学习进度分布</span>
              <el-select v-model="distributionSubject" size="small">
                <el-option label="科目一" value="subject1" />
                <el-option label="科目二" value="subject2" />
                <el-option label="科目三" value="subject3" />
                <el-option label="科目四" value="subject4" />
              </el-select>
            </div>
          </template>
          <div class="chart-container" ref="distributionChartRef"></div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="chart-row">
      <!-- 教练学员分布 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>教练学员分布</span>
            </div>
          </template>
          <div class="chart-container" ref="coachStudentChartRef"></div>
        </el-card>
      </el-col>

      <!-- 学习效果分析 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>学习效果分析</span>
            </div>
          </template>
          <div class="chart-container" ref="effectChartRef"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import {
  User,
  Timer,
  Medal,
  DataLine
} from '@element-plus/icons-vue'
import * as echarts from 'echarts'

// 日期快捷选项
const dateShortcuts = [
  {
    text: '最近一周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    }
  },
  {
    text: '最近一月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    }
  },
  {
    text: '最近三月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    }
  }
]

// 筛选表单
const filterForm = reactive({
  dateRange: [],
  subject: ''
})

// 统计卡片数据
const statisticsCards = [
  {
    title: '总学员数',
    value: '1,234',
    icon: User,
    color: '#409EFF'
  },
  {
    title: '总学时',
    value: '5,678',
    icon: Timer,
    color: '#67C23A'
  },
  {
    title: '考试通过率',
    value: '92.5%',
    icon: Medal,
    color: '#E6A23C'
  },
  {
    title: '平均学时',
    value: '48.5',
    icon: DataLine,
    color: '#F56C6C'
  }
]

// 图表相关
const trendTimeRange = ref('month')
const distributionSubject = ref('subject2')

const trendChartRef = ref(null)
const distributionChartRef = ref(null)
const coachStudentChartRef = ref(null)
const effectChartRef = ref(null)

let trendChart = null
let distributionChart = null
let coachStudentChart = null
let effectChart = null

// 初始化趋势图
const initTrendChart = () => {
  trendChart = echarts.init(trendChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['科目一', '科目二', '科目三', '科目四']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: {
      type: 'value',
      name: '学时(小时)'
    },
    series: [
      {
        name: '科目一',
        type: 'line',
        smooth: true,
        data: [120, 132, 101, 134, 90, 230, 210]
      },
      {
        name: '科目二',
        type: 'line',
        smooth: true,
        data: [220, 182, 191, 234, 290, 330, 310]
      },
      {
        name: '科目三',
        type: 'line',
        smooth: true,
        data: [150, 232, 201, 154, 190, 330, 410]
      },
      {
        name: '科目四',
        type: 'line',
        smooth: true,
        data: [320, 332, 301, 334, 390, 330, 320]
      }
    ]
  }
  trendChart.setOption(option)
}

// 初始化分布图
const initDistributionChart = () => {
  distributionChart = echarts.init(distributionChartRef.value)
  const option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '进度分布',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 35, name: '已完成' },
          { value: 45, name: '进行中' },
          { value: 15, name: '未开始' },
          { value: 5, name: '已暂停' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  distributionChart.setOption(option)
}

// 初始化教练学员分布图
const initCoachStudentChart = () => {
  coachStudentChart = echarts.init(coachStudentChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      data: ['在培学员数', '结业学员数']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: ['李教练', '王教练', '张教练', '赵教练', '孙教练']
    },
    series: [
      {
        name: '在培学员数',
        type: 'bar',
        stack: 'total',
        label: {
          show: true
        },
        emphasis: {
          focus: 'series'
        },
        data: [15, 12, 18, 10, 14]
      },
      {
        name: '结业学员数',
        type: 'bar',
        stack: 'total',
        label: {
          show: true
        },
        emphasis: {
          focus: 'series'
        },
        data: [25, 30, 20, 28, 22]
      }
    ]
  }
  coachStudentChart.setOption(option)
}

// 初始化学习效果分析图
const initEffectChart = () => {
  effectChart = echarts.init(effectChartRef.value)
  const option = {
    radar: {
      indicator: [
        { name: '理论掌握', max: 100 },
        { name: '实操技能', max: 100 },
        { name: '学习态度', max: 100 },
        { name: '出勤率', max: 100 },
        { name: '考试成绩', max: 100 }
      ]
    },
    series: [
      {
        name: '学习效果分析',
        type: 'radar',
        data: [
          {
            value: [85, 90, 95, 88, 92],
            name: '平均水平'
          },
          {
            value: [95, 88, 92, 95, 96],
            name: '优秀学员'
          }
        ]
      }
    ]
  }
  effectChart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  trendChart?.resize()
  distributionChart?.resize()
  coachStudentChart?.resize()
  effectChart?.resize()
}

// 筛选
const handleFilter = () => {
  // TODO: 实现筛选逻辑
  console.log('筛选条件：', filterForm)
}

// 重置筛选
const resetFilter = () => {
  Object.keys(filterForm).forEach(key => {
    filterForm[key] = ''
  })
  handleFilter()
}

onMounted(() => {
  // 初始化图表
  initTrendChart()
  initDistributionChart()
  initCoachStudentChart()
  initEffectChart()

  // 添加窗口大小变化监听
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  // 移除窗口大小变化监听
  window.removeEventListener('resize', handleResize)

  // 销毁图表实例
  trendChart?.dispose()
  distributionChart?.dispose()
  coachStudentChart?.dispose()
  effectChart?.dispose()
})
</script>

<style scoped>
.learning-analysis {
  padding: 20px;
}

.filter-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
}

.statistics-cards {
  margin-bottom: 20px;
}

.statistics-card-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.card-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
}

.card-icon :deep(.el-icon) {
  font-size: 24px;
}

.card-info {
  flex: 1;
}

.card-value {
  font-size: 24px;
  font-weight: bold;
  line-height: 1.2;
  margin-bottom: 4px;
}

.card-title {
  font-size: 14px;
  color: #909399;
}

.chart-row {
  margin-bottom: 20px;
}

.chart-card {
  height: 400px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-container {
  height: 320px;
}
</style>